package demo.gaobo.com.myapplication.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.ButterKnife;
import demo.gaobo.com.myapplication.R;


public abstract class BaseActivity<V, T extends BasePresenter<V>> extends AppCompatActivity implements View.OnClickListener {


    protected T mPresenter;
    ImageView ivBaseBack;
    TextView tvBaseTitle;
    private TextView tv_base_second_title;
    Toolbar baseToolbar;
    FrameLayout flBase;

    protected String TAG;
    private ImageView ivBaseSecondIcon;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //禁用横竖屏切换
        // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        init();
        //判断是否使用MVP模式
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
        }

        //子类不再需要设置布局ID，也不再需要使用ButterKnife.bind()
        if (setToolBarTitle() != null) {
            setContentView(R.layout.activity_base);
            flBase = (FrameLayout) findViewById(R.id.fl_base);
            baseToolbar = (Toolbar) findViewById(R.id.base_toolbar);
            tvBaseTitle = (TextView) findViewById(R.id.tv_base_title);
            ivBaseBack = (ImageView) findViewById(R.id.iv_base_back);
            ivBaseSecondIcon = (ImageView) findViewById(R.id.iv_base_second_icon);
            tv_base_second_title = (TextView) findViewById(R.id.tv_base_second_title);
            View contentLayout = LayoutInflater.from(this).inflate(provideContentViewId(), null);
            if (flBase != null) {
                flBase.addView(contentLayout);
            }
            if (!setToolBarSecondTitle().equals("")) {
                tv_base_second_title.setVisibility(View.VISIBLE);
                tv_base_second_title.setText(setToolBarSecondTitle());
                tv_base_second_title.setOnClickListener(this);
            }

            if (setToolBarSecondIcon() != 0) {
                ivBaseSecondIcon.setVisibility(View.VISIBLE);
               // Glide.with(this).load(setToolBarSecondIcon()).into(ivBaseSecondIcon);
                ivBaseSecondIcon.setOnClickListener(this);
            }
            tvBaseTitle.setText(setToolBarTitle());
            ivBaseBack.setOnClickListener(this);
        } else {
            setContentView(provideContentViewId());
        }
        ButterKnife.bind(this);

        //initStatusBar(R.color.TabTextColor);
        excuteStatesBar();

        initView();
        savedInstanceState(savedInstanceState);
        initData();
        initListener();
    }

    protected void savedInstanceState(Bundle savedInstanceState) {
    }

    /**
     * 解决4.4设置状态栏颜色之后，布局内容嵌入状态栏位置问题
     */
    private void excuteStatesBar() {
        ViewGroup mContentView = (ViewGroup) getWindow().findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows,
            // 而是设置 ContentView 的第一个子 View ，预留出系统 View 的空间.
            mChildView.setFitsSystemWindows(true);
        }
    }

    /**
     * 设置沉浸式状态栏的方法
     */
    protected void initStatusBar(int color) {
        //如果想支持4.4以上版本，需要将状态栏设置成透明的才能着色。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //KITKAT表示是4.4版本
            Window win = getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            //WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS含义为状态栏透明
            winParams.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            win.setAttributes(winParams);
        }
  /*      SystemBarTintManager tintManager = new SystemBarTintManager(this); //着色管理器
        tintManager.setStatusBarTintEnabled(true);//设置支持着色
        tintManager.setStatusBarTintColor(color);//指设置颜色*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       // App.activities.remove(this);
        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    //在setContentView()调用之前调用，可以设置WindowFeature(如：this.requestWindowFeature(Window.FEATURE_NO_TITLE);)
    public void init() {
        TAG = this.getClass().getName();
    }

    public void initView() {
    }

    public void initData() {
    }

    public void initListener() {
    }

    //用于创建Presenter和判断是否使用MVP模式(由子类实现)
    protected abstract T createPresenter();

    //得到当前界面的布局文件id(由子类实现)
    protected abstract int provideContentViewId();

    //是否创建ToolBar,标题设置
    protected abstract String setToolBarTitle();

    //是否创建ToolBar,标题设置
    protected String setToolBarSecondTitle() {
        return "";
    }

    //是否创建ToolBar副标题图标设置
    protected int setToolBarSecondIcon() {
        return 0;
    }



    //返回按钮点击事件
    public void onBackIconClicked() {
        this.finish();
    }

    //返回按钮点击事件
    public void onSecondTitleClicked() {

    }
    //副标题按钮点击事件
    public void onSecondIconClicked() {

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_base_back:
                onBackIconClicked();
                break;
            case R.id.tv_base_second_title:
                onSecondTitleClicked();
                break;
            case R.id.iv_base_second_icon:
                onSecondIconClicked();
                break;
        }


    }


    @Override
    public void onBackPressed() {
        onBackIconClicked();
    }
}
